package lego.diagram.navigator;

import lego.LightSensor;
import lego.MicroSensor;
import lego.MotorActioner;
import lego.Robot;
import lego.TouchSensor;
import lego.UltraSoundSensor;
import lego.diagram.edit.parts.ActionerPortActionerEditPart;
import lego.diagram.edit.parts.ActionerPortEditPart;
import lego.diagram.edit.parts.LightSensorEditPart;
import lego.diagram.edit.parts.LightSensorNameEditPart;
import lego.diagram.edit.parts.MicroSensorEditPart;
import lego.diagram.edit.parts.MicroSensorNameEditPart;
import lego.diagram.edit.parts.MotorActionerEditPart;
import lego.diagram.edit.parts.MotorActionerNameEditPart;
import lego.diagram.edit.parts.PicoNetEditPart;
import lego.diagram.edit.parts.PicoNetMasterEditPart;
import lego.diagram.edit.parts.PicoNetSlavesEditPart;
import lego.diagram.edit.parts.RobotEditPart;
import lego.diagram.edit.parts.RobotNameEditPart;
import lego.diagram.edit.parts.RootEditPart;
import lego.diagram.edit.parts.SensorPortEditPart;
import lego.diagram.edit.parts.SensorPortSensorEditPart;
import lego.diagram.edit.parts.TouchSensorEditPart;
import lego.diagram.edit.parts.TouchSensorNameEditPart;
import lego.diagram.edit.parts.UltraSoundSensorEditPart;
import lego.diagram.edit.parts.UltraSoundSensorNameEditPart;
import lego.diagram.part.LegoDiagramEditorPlugin;
import lego.diagram.part.LegoVisualIDRegistry;
import lego.diagram.providers.LegoElementTypes;
import lego.diagram.providers.LegoParserProvider;

import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

/**
 * @generated
 */
public class LegoNavigatorLabelProvider extends LabelProvider implements
		ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		LegoDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		LegoDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof LegoNavigatorItem
				&& !isOwnView(((LegoNavigatorItem) element).getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof LegoNavigatorGroup) {
			LegoNavigatorGroup group = (LegoNavigatorGroup) element;
			return LegoDiagramEditorPlugin.getInstance().getBundledImage(
					group.getIcon());
		}

		if (element instanceof LegoNavigatorItem) {
			LegoNavigatorItem navigatorItem = (LegoNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (LegoVisualIDRegistry.getVisualID(view)) {
		case ActionerPortActionerEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://fr.ubo.mde.lucava.lego?ActionerPort?actioner", LegoElementTypes.ActionerPortActioner_4004); //$NON-NLS-1$
		case MicroSensorEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://fr.ubo.mde.lucava.lego?MicroSensor", LegoElementTypes.MicroSensor_2001); //$NON-NLS-1$
		case PicoNetMasterEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://fr.ubo.mde.lucava.lego?PicoNet?master", LegoElementTypes.PicoNetMaster_4002); //$NON-NLS-1$
		case RobotEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://fr.ubo.mde.lucava.lego?Robot", LegoElementTypes.Robot_2006); //$NON-NLS-1$
		case PicoNetSlavesEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://fr.ubo.mde.lucava.lego?PicoNet?slaves", LegoElementTypes.PicoNetSlaves_4003); //$NON-NLS-1$
		case ActionerPortEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://fr.ubo.mde.lucava.lego?ActionerPort", LegoElementTypes.ActionerPort_3001); //$NON-NLS-1$
		case SensorPortEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://fr.ubo.mde.lucava.lego?SensorPort", LegoElementTypes.SensorPort_3002); //$NON-NLS-1$
		case LightSensorEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://fr.ubo.mde.lucava.lego?LightSensor", LegoElementTypes.LightSensor_2005); //$NON-NLS-1$
		case MotorActionerEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://fr.ubo.mde.lucava.lego?MotorActioner", LegoElementTypes.MotorActioner_2007); //$NON-NLS-1$
		case SensorPortSensorEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://fr.ubo.mde.lucava.lego?SensorPort?sensor", LegoElementTypes.SensorPortSensor_4001); //$NON-NLS-1$
		case RootEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?http://fr.ubo.mde.lucava.lego?Root", LegoElementTypes.Root_1000); //$NON-NLS-1$
		case TouchSensorEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://fr.ubo.mde.lucava.lego?TouchSensor", LegoElementTypes.TouchSensor_2004); //$NON-NLS-1$
		case UltraSoundSensorEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://fr.ubo.mde.lucava.lego?UltraSoundSensor", LegoElementTypes.UltraSoundSensor_2003); //$NON-NLS-1$
		case PicoNetEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://fr.ubo.mde.lucava.lego?PicoNet", LegoElementTypes.PicoNet_2002); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = LegoDiagramEditorPlugin.getInstance()
				.getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null && elementType != null
				&& LegoElementTypes.isKnownElementType(elementType)) {
			image = LegoElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof LegoNavigatorGroup) {
			LegoNavigatorGroup group = (LegoNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof LegoNavigatorItem) {
			LegoNavigatorItem navigatorItem = (LegoNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (LegoVisualIDRegistry.getVisualID(view)) {
		case ActionerPortActionerEditPart.VISUAL_ID:
			return getActionerPortActioner_4004Text(view);
		case MicroSensorEditPart.VISUAL_ID:
			return getMicroSensor_2001Text(view);
		case PicoNetMasterEditPart.VISUAL_ID:
			return getPicoNetMaster_4002Text(view);
		case RobotEditPart.VISUAL_ID:
			return getRobot_2006Text(view);
		case PicoNetSlavesEditPart.VISUAL_ID:
			return getPicoNetSlaves_4003Text(view);
		case ActionerPortEditPart.VISUAL_ID:
			return getActionerPort_3001Text(view);
		case SensorPortEditPart.VISUAL_ID:
			return getSensorPort_3002Text(view);
		case LightSensorEditPart.VISUAL_ID:
			return getLightSensor_2005Text(view);
		case MotorActionerEditPart.VISUAL_ID:
			return getMotorActioner_2007Text(view);
		case SensorPortSensorEditPart.VISUAL_ID:
			return getSensorPortSensor_4001Text(view);
		case RootEditPart.VISUAL_ID:
			return getRoot_1000Text(view);
		case TouchSensorEditPart.VISUAL_ID:
			return getTouchSensor_2004Text(view);
		case UltraSoundSensorEditPart.VISUAL_ID:
			return getUltraSoundSensor_2003Text(view);
		case PicoNetEditPart.VISUAL_ID:
			return getPicoNet_2002Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getPicoNet_2002Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getRoot_1000Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getLightSensor_2005Text(View view) {
		IParser parser = LegoParserProvider
				.getParser(LegoElementTypes.LightSensor_2005,
						view.getElement() != null ? view.getElement() : view,
						LegoVisualIDRegistry
								.getType(LightSensorNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			LegoDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5004); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getMicroSensor_2001Text(View view) {
		IParser parser = LegoParserProvider
				.getParser(LegoElementTypes.MicroSensor_2001,
						view.getElement() != null ? view.getElement() : view,
						LegoVisualIDRegistry
								.getType(MicroSensorNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			LegoDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getUltraSoundSensor_2003Text(View view) {
		IParser parser = LegoParserProvider.getParser(
				LegoElementTypes.UltraSoundSensor_2003,
				view.getElement() != null ? view.getElement() : view,
				LegoVisualIDRegistry
						.getType(UltraSoundSensorNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			LegoDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5002); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getTouchSensor_2004Text(View view) {
		IParser parser = LegoParserProvider
				.getParser(LegoElementTypes.TouchSensor_2004,
						view.getElement() != null ? view.getElement() : view,
						LegoVisualIDRegistry
								.getType(TouchSensorNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			LegoDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPicoNetMaster_4002Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getActionerPortActioner_4004Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getMotorActioner_2007Text(View view) {
		IParser parser = LegoParserProvider.getParser(
				LegoElementTypes.MotorActioner_2007,
				view.getElement() != null ? view.getElement() : view,
				LegoVisualIDRegistry
						.getType(MotorActionerNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			LegoDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5006); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getSensorPortSensor_4001Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getActionerPort_3001Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getSensorPort_3002Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getRobot_2006Text(View view) {
		IParser parser = LegoParserProvider.getParser(
				LegoElementTypes.Robot_2006,
				view.getElement() != null ? view.getElement() : view,
				LegoVisualIDRegistry.getType(RobotNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			LegoDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5005); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getPicoNetSlaves_4003Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return RootEditPart.MODEL_ID.equals(LegoVisualIDRegistry
				.getModelID(view));
	}

}
